package com.jingyanzi.member.controller;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jingyanzi.member.api.SessionClientAPI;
import com.jingyanzi.member.domain.CustomerDO;
import com.jingyanzi.member.domain.CustomerExtraDO;
import com.jingyanzi.member.domain.LoginDO;
import com.jingyanzi.member.domain.SelectDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.query.CustomerQueryDO;
import com.jingyanzi.member.request.SingleRequest;
import com.jingyanzi.member.response.ListResponse;
import com.jingyanzi.member.response.PageResponse;
import com.jingyanzi.member.response.SingleResponse;
import com.jingyanzi.member.service.ICustomerExtraService;
import com.jingyanzi.member.service.ICustomerService;
import com.jingyanzi.member.util.StaticDicUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "客户管理controller", tags = { "客户操作接口" })
@RestController
@RequestMapping("/")
public class CustomerController {

	public static final String SYS_ERROR = "{status:500, msg:\"系统错误\", version:1}";

	public static final String DES = "3DES";

	public static final String PLANINTEXT = "plaintext";

	public static Map<Integer, String> customerStatus = new HashMap<Integer, String>();

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private ICustomerExtraService customerExtraService;

	@Autowired
	private SessionClientAPI sessionClientAPI;

	private LoginDO getLoginDO(String token) {
		Base64.Encoder encoder = Base64.getEncoder();
		LoginDO loginDO = new LoginDO();
		try {
			String t = encoder.encodeToString(token.getBytes("utf-8"));
			loginDO = sessionClientAPI.getSession(t);
			if (loginDO == null) {
				loginDO = new LoginDO();
				loginDO.setId(1);
				loginDO.setMobile("13800000000");
				loginDO.setNickname("罗于红");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return loginDO;
	}

	@ApiOperation(value = "静态字典", notes = "状态静态字典下啦菜单")
	@ApiImplicitParams({
	})
	@RequestMapping(value = "/dictSelect.json", method = RequestMethod.POST)
	public ListResponse<SelectDO> dictSelect(@RequestBody SingleRequest<SelectDO> request) {
		SelectDO selectDO = request.getData();
		ListResponse<SelectDO> response = new ListResponse<SelectDO>();
		List<SelectDO> selectList = new ArrayList<SelectDO>();

		if (StaticDicUtil.staticMap.containsKey(selectDO.getCode())) {
			Map<Integer, String> dataMap = StaticDicUtil.staticMap.get(selectDO.getCode());
			for (Integer key : dataMap.keySet()) {
				SelectDO data = new SelectDO();
				data.setValue(key);
				data.setLabel(dataMap.get(key));
				selectList.add(data);
			}
			response.setData(selectList);
		}

		return response;
	}

	@ApiOperation(value = "状态静态字典", notes = "状态静态字典下啦菜单")
	@ApiImplicitParams({

	})
	@RequestMapping(value = "/statusSelect.json", method = RequestMethod.POST)
	public ListResponse<SelectDO> getStatus(HttpServletRequest request) {
		ListResponse<SelectDO> response = new ListResponse<SelectDO>();
		List<SelectDO> selectList = new ArrayList<SelectDO>();

		for (Integer key : StaticDicUtil.STATUS_CUSTOMER.keySet()) {
			SelectDO selectDO = new SelectDO();
			selectDO.setValue(key);
			selectDO.setLabel(StaticDicUtil.STATUS_CUSTOMER.get(key));
			selectList.add(selectDO);
		}
		response.setData(selectList);

		return response;
	}

	@ApiOperation(value = "创建客户", notes = "创建客户")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户对象", required = true, dataType = "SingleRequest<CustomerDO>") })
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public SingleResponse<CustomerDO> add(@RequestBody SingleRequest<CustomerDO> request) {

		CustomerDO domain = request.getData();
		if (domain.getAccountBalance() == null) {
			domain.setAccountBalance(new BigDecimal(0.00));
		}

		domain.setAuthorId(getLoginDO(request.getToken()).getId());
		domain.setAuthor(getLoginDO(request.getToken()).getNickname());
		domain.setEmployeeMobile(getLoginDO(request.getToken()).getMobile());
		domain.setEmployeeName(domain.getAuthor());

		ResultDO result = customerService.add(domain);

		SingleResponse<CustomerDO> response = new SingleResponse<CustomerDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("添加成功");
			response.setData((CustomerDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("添加失败");
		}
		return response;
	}

	@ApiOperation(value = "修改客户基本信息", notes = "修改客户基本信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户对象", required = true, dataType = "SingleRequest<CustomerDO>") })
	@RequestMapping(value = "/edit.json", method = RequestMethod.POST)
	public SingleResponse<CustomerDO> edit(@RequestBody SingleRequest<CustomerDO> request) {

		CustomerDO domain = request.getData();
		System.out.println(domain);
		if (domain.getAccountBalance() == null) {
			domain.setAccountBalance(new BigDecimal(0.00));
		}
		ResultDO result = customerService.modifi(domain);
		SingleResponse<CustomerDO> response = new SingleResponse<CustomerDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("修改成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("修改失败");
		}
		return response;
	}

	@ApiOperation(value = "修改客户扩展信息", notes = "修改用扩展信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户对象", required = true, dataType = "SingleRequest<CustomerExtraDO>") })
	@RequestMapping(value = "/edit_extra.json", method = RequestMethod.POST)
	public SingleResponse<CustomerExtraDO> editExtra(@RequestBody SingleRequest<CustomerExtraDO> request) {

		CustomerExtraDO domain = request.getData();
		if (domain.getConsumeMoney() == null) {
			domain.setConsumeMoney(new BigDecimal(0.00));
		}
		ResultDO result = customerExtraService.edit(domain);
		SingleResponse<CustomerExtraDO> response = new SingleResponse<CustomerExtraDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("修改成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("修改失败");
		}
		return response;
	}

	@ApiOperation(value = "获取客户详情", notes = "获取客户详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户对象(只传客户的ID)", required = true, dataType = "SingleRequest<CustomerDO>") })
	@RequestMapping(value = "/detail.json", method = RequestMethod.POST)
	public SingleResponse<Map<String, Object>> detail(@RequestBody SingleRequest<CustomerDO> request) {

		CustomerDO domain = request.getData();

		ResultDO result = customerService.get(domain.getId());
		SingleResponse<Map<String, Object>> response = new SingleResponse<Map<String, Object>>();

		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("获取成功");
			response.setData((Map<String, Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("获取失败");
		}
		return response;
	}

	@ApiOperation(value = "修改客户扩展信息", notes = "修改用扩展信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户对象(只传客户的ID)", required = true, dataType = "SingleRequest<CustomerDO>") })
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public SingleResponse<CustomerDO> delete(@RequestBody SingleRequest<CustomerDO> request) {

		CustomerDO domain = request.getData();

		ResultDO result = customerService.remove(domain.getIds());
		SingleResponse<CustomerDO> response = new SingleResponse<CustomerDO>();
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("删除成功");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("删除失败");
		}
		return response;
	}

	@ApiOperation(value = "查询客户信息", notes = "查询客户展信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "request", value = "客户查询对象", required = true, dataType = "SingleRequest<CustomerQueryDO>") })
	@RequestMapping(value = "/query.json", method = RequestMethod.POST)
	public PageResponse<CustomerDO> query(@RequestBody SingleRequest<CustomerQueryDO> request) {

		CustomerQueryDO query = request.getData();
		query.setPage(true);
		ResultDO result = customerService.query(query);

		PageResponse<CustomerDO> response = new PageResponse<CustomerDO>();
		if (result.isSuccess()) {
			response.setPageindex(query.getPageNO());
			response.setPagesize(query.getPageRows());
			response.setTotal((Long) result.getModel(ResultSupport.SECOND_MODEL_KEY));
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
			response.setData((List<CustomerDO>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("查询失败");
		}
		return response;
	}
}
